package org.nanhill.xsheet.output.xhtml;

import static org.nanhill.commons.html.XhtmlConstants.BORDER;
import static org.nanhill.commons.html.XhtmlConstants.COLSPAN;
import static org.nanhill.commons.html.XhtmlConstants.ROWSPAN;
import static org.nanhill.commons.html.XhtmlConstants.TABLE;
import static org.nanhill.commons.html.XhtmlConstants.TABLE_END;
import static org.nanhill.commons.html.XhtmlConstants.TD;
import static org.nanhill.commons.html.XhtmlConstants.TD_END;
import static org.nanhill.commons.html.XhtmlConstants.TR_END;
import static org.nanhill.commons.html.XhtmlConstants.TR_START;
import static org.nanhill.commons.lang.StringConstants.BLANK_CHAR;
import static org.nanhill.commons.lang.StringConstants.EQUAL;
import static org.nanhill.commons.lang.StringConstants.GT;
import static org.nanhill.commons.lang.StringConstants.LT;
import static org.nanhill.commons.lang.StringConstants.QUOT;

import java.io.IOException;
import java.io.OutputStream;

import org.nanhill.commons.model.table.Cell;
import org.nanhill.commons.model.table.Row;
import org.nanhill.commons.model.table.Table;

public class DefaultXhtmlOutputBuilder extends XhtmlOutputBuilder {

	@Override
	protected void buildTables() {
		for (Table table : this.model.getTables()) {
			this.buildTable(table);
		}
	}

	@Override
	protected void buildTable(Table table) {
		this.htmlBuilder.append(LT);
		this.htmlBuilder.append(TABLE);
		this.htmlBuilder.append(BLANK_CHAR);
		this.htmlBuilder.append(BORDER);
		this.htmlBuilder.append(EQUAL);
		this.htmlBuilder.append(QUOT);
		this.htmlBuilder.append(1);
		this.htmlBuilder.append(QUOT);
		this.htmlBuilder.append(GT);
		for (Row tr : table.getRows()) {
			this.buildTr(tr);
		}
		this.htmlBuilder.append(TABLE_END);

	}

	@Override
	protected void buildTr(Row row) {
		this.htmlBuilder.append(TR_START);
		for (Cell td : row.getCells()) {
			this.buildTd(td);
		}
		this.htmlBuilder.append(TR_END);
	}

	/*
	 * 
	 * 
	 * @see org.nanhill.xsheet.output.html.XSheetHtmlOutputBuilder#buildTd(org.nanhill.commons.model.table.Cell,
	 *      boolean)
	 */
	@Override
	protected void buildTd(Cell cell) {
		if (cell == null)
			return;
		this.htmlBuilder.append(LT);
		this.htmlBuilder.append(TD);
		if (cell.getColspan() != 1) {
			this.htmlBuilder.append(BLANK_CHAR);
			this.htmlBuilder.append(COLSPAN);
			this.htmlBuilder.append(EQUAL);
			this.htmlBuilder.append(QUOT);
			this.htmlBuilder.append(cell.getColspan());
			this.htmlBuilder.append(QUOT);
		}
		if (cell.getRowspan() != 1) {
			this.htmlBuilder.append(BLANK_CHAR);
			this.htmlBuilder.append(ROWSPAN);
			this.htmlBuilder.append(EQUAL);
			this.htmlBuilder.append(QUOT);
			this.htmlBuilder.append(cell.getRowspan());
			this.htmlBuilder.append(QUOT);
		}
		this.htmlBuilder.append(GT);
		this.htmlBuilder.append(cell.getValue());
		this.htmlBuilder.append(TD_END);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.nanhill.xsheet.output.OutputBuilder#write(java.io.OutputStream)
	 */
	public void write(OutputStream os) throws IOException {
		String out = this.getProduct();
		os.write(out.getBytes());

	}

}
